package com.zc.im.service.im.group.impl;

import com.github.pagehelper.PageHelper;
import com.zc.im.mapper.im.group.ZcImMsgHistoryMapper;
import com.zc.im.model.im.group.ZcImMsgHistoryExample;
import com.zc.im.model.im.group.ZcImMsgHistoryModel;
import com.zc.im.service.im.group.ImMsgHistoryService;
import com.zc.im.util.DateUtils;
import com.zc.im.util.ImGolbalTypeContext;
import com.zc.im.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * Created by Administrator on 2017/7/7.
 */
@Service
public class ImMsgHistoryServiceImpl implements ImMsgHistoryService {

    @Autowired
    private ZcImMsgHistoryMapper msgHistoryMapper;


    /**
     * 查询消息历史
     * 2017-7-7 15:54:48
     *
     * @param msgHistoryModel
     * @return
     */
    public List<ZcImMsgHistoryModel> selectImMsgHistorys(ZcImMsgHistoryModel msgHistoryModel, Integer pageNum , Integer pageSize) {
        List<ZcImMsgHistoryModel> historyModels = new ArrayList<>();
        if (msgHistoryModel == null) {
            return historyModels;
        }
        PageHelper.startPage(ImGolbalTypeContext.PAGEHELPER_PAGENUM, ImGolbalTypeContext.PAGEHELPER_PAGESIZE);
        ZcImMsgHistoryExample example = new ZcImMsgHistoryExample();
        ZcImMsgHistoryExample.Criteria criteria = example.createCriteria();


        //消息类型
        if(!StringUtils.isEmpty(msgHistoryModel.getMsgType())){
            criteria.andMsgTypeEqualTo(msgHistoryModel.getMsgType());
        }
        //消息开始时间    结束时间
        if(!StringUtils.isEmpty(msgHistoryModel.getStartTime()) && !StringUtils.isEmpty(msgHistoryModel.getEndTime())) {
            criteria.andCreatedTimeBetween(DateUtils.strFormatDateYmdHms(msgHistoryModel.getStartTime()),
                    DateUtils.strFormatDateYmdHms(msgHistoryModel.getEndTime()));
        }
        msgHistoryModel.setOrderByClause(" created_time desc");
        PageHelper.startPage(pageNum, pageSize);
        historyModels = msgHistoryMapper.selectMsgOrByExample(msgHistoryModel);
        if(historyModels == null || historyModels.size() ==0){
            return historyModels;
        }
        TreeMap<Integer, ZcImMsgHistoryModel> historyMap = new TreeMap();
        for(ZcImMsgHistoryModel historyModel: historyModels){
            historyMap.put(historyModel.getId(), historyModel);
        }
        historyModels.clear();
        for(Map.Entry<Integer, ZcImMsgHistoryModel> entry: historyMap.entrySet()){
            historyModels.add(entry.getValue());
        }
        return historyModels;
    }

    public Integer insertImMsgHistory(ZcImMsgHistoryModel msgHistoryModel) {
        if (msgHistoryModel == null) {
            return 0;
        }
        return msgHistoryMapper.insertSelective(msgHistoryModel);
    }


    public Integer updateMsgHistory(ZcImMsgHistoryModel msgHistoryCriteria){
        Integer updateFlag = 0;
        if(msgHistoryCriteria == null){
            return updateFlag;
        }
        ZcImMsgHistoryExample historyExample = new ZcImMsgHistoryExample();
        ZcImMsgHistoryExample.Criteria criteria =  historyExample.createCriteria();

        if(!StringUtils.isEmpty(msgHistoryCriteria.getMsgAgreementType())){
            criteria.andMsgAgreementTypeEqualTo(msgHistoryCriteria.getMsgAgreementType());
        }
        if(msgHistoryCriteria.getSourceObjId()!= null){
            criteria.andSourceObjIdEqualTo(msgHistoryCriteria.getSourceObjId());
        }
        if(msgHistoryCriteria.getTargetObjId()!= null){
            criteria.andTargetObjIdEqualTo(msgHistoryCriteria.getTargetObjId());
        }
        updateFlag = msgHistoryMapper.updateByExampleSelective(msgHistoryCriteria, historyExample);
        return updateFlag;
    }


}
